import java.sql.*;

public class jdbc_set_get_object_case0024 {

    public static Connection GetConnection(String username,String passwd){
        String driver = "org.postgresql.Driver";
        String sourceURL = "jdbc:postgresql://host:dbport/dbname";
        Connection conn = null;
        try {
            Class.forName("org.postgresql.Driver");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        try {
            conn = DriverManager.getConnection(sourceURL,username,passwd);
            System.out.println("Connection succeed");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return conn;
    }

    /**
     * 查找数据
     */
    public static void query(Connection conn) throws SQLException{
        String sql = "select current_database();";
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
		System.out.println("current database is:");
        while (resultSet.next()) {
			String database = resultSet.getString("current_database");
            System.out.println(database);
        }
        conn.commit();
    }

    public static void main(String[] args) throws SQLException {
        Connection conn = GetConnection("usrname","password");
        query(conn);
        conn.close();
    }
}
